package com.onluy.provider.department.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.onluy.frameworkcloud.Param.ReqHead;
import com.onluy.frameworkcloud.Param.ReqInfo;
import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.frameworkcloud.tools.utils.StringUtil;
import com.onluy.frameworkcloud.Param.ParamPagination;
import  com.onluy.provider.department.entity.SysUser;
import com.onluy.frameworkcloud.base.AppController;
import com.onluy.frameworkcloud.base.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import  com.onluy.provider.department.service.SysUserService;
import javax.validation.Valid;

@Validated
@RestController
@RequestMapping("/user")
@Api(value="user", description="/用户管理接口", position=1 )
public class SysUserController extends AppController<SysUser> {


	//调用服务
	@Autowired
	private SysUserService sysUserService;

	@Override
	public AppService service() {
		return sysUserService;
	}


	@RequestMapping(value={"/target"},method={ RequestMethod.POST})
	@ApiOperation(value="目标", notes="根据webName获取用户信息")
	public RspInfo<SysUser> dotarget(@RequestBody   ReqInfo<ReqHead,SysUser> reqInfo   ) throws Exception {
		log.error("dotarget sysUser:"+ JSON.toJSONString(reqInfo));
		if(StringUtil.isEmpty(reqInfo.getReqBody().getWebName())){
			throw new Exception("webName不能为空");
		}
		return super.Model(reqInfo.getReqBody());
	}


	@RequestMapping(value={ "/add"},method={RequestMethod.POST })
	@ApiOperation(value="新增", notes="新增用户")
	public RspInfo<InParam> doAdd(@RequestBody @Valid  ReqInfo<ReqHead,SysUser> reqInfo ) {
		log.error("dotarget sysUser:"+ JSON.toJSONString(reqInfo));
		if(reqInfo.getReqBody().getDepartId()==null ||reqInfo.getReqBody().getDepartId() ==null) {
			reqInfo.getReqBody().setDepartId((long)2) ;
		}
		return super.Add(reqInfo.getReqBody());
	}

	@RequestMapping(value={ "/update"},method={RequestMethod.POST})
    @ApiOperation(value="更新", notes="更新用户")
	public RspInfo<InParam> doUpdate(      @RequestBody @Valid  ReqInfo<ReqHead,SysUser> reqInfo ) {
		log.error("dotarget sysUser:"+ JSON.toJSONString(reqInfo));
		return super.Update(reqInfo.getReqBody());
	}

	@RequestMapping(value={"/del"},method={RequestMethod.POST})
	@ApiOperation(value="删除", notes="删除用户")
	public RspInfo<InParam> doDel( @RequestBody    ReqInfo<ReqHead,SysUser> reqInfo) throws Exception {
		log.error("dotarget sysUser:"+ JSON.toJSONString(reqInfo));
		if(reqInfo.getReqBody().getUserId()==null || reqInfo.getReqBody().getUserId()<=0){
			throw new Exception("用户ID不能为空");
		}
		return super.Del(reqInfo.getReqBody());
	}


	@ResponseBody
	@RequestMapping(value={ "/list"},method={RequestMethod.POST})
	@ApiOperation(value="列表", notes="用户列表明细")
	public RspInfo<PageInfo<SysUser>>  doList(     @RequestBody    ReqInfo<ParamPagination,SysUser> reqInfo ) {
		log.error("dotarget sysUser:"+ JSON.toJSONString(reqInfo));
		if(reqInfo.getReqBody().getDepartId()==null || reqInfo.getReqBody().getDepartId() <0 ) {
			reqInfo.getReqBody().setDepartId(    Long.parseLong("2")  );
		}
		return super.List(reqInfo.getReqBody(),reqInfo.getReqHead() );

	}



	@RequestMapping(value={ "/simplelist"},method={RequestMethod.POST})
	@ApiOperation(value="简要列表", notes="用户列表明细")
	public RspInfo<PageInfo<SysUser>> dosimplelist( @RequestBody    ReqInfo<ParamPagination,SysUser> reqInfo ) {
		log.error("dotarget sysUser:"+ JSON.toJSONString(reqInfo));
		return  super.simplelist( reqInfo.getReqBody(),reqInfo.getReqHead() );
	}


}
